<template>
  <div class="app-container">
    <el-form label-width="120px" :model="teacher" :rules="formRules" ref="dataForm">
      <el-form-item label="教师名称" prop="name">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="教师职称">
        <el-input v-model="teacher.jobTitle"/>
      </el-form-item>
      <el-form-item label="教师性别">
        <el-select v-model="teacher.gender" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
          <el-option :value=0 label="女"/>
          <el-option :value=1 label="男"/>
        </el-select>
      </el-form-item>
      <el-form-item label="教师简介" prop="intro">
        <el-input v-model="teacher.intro" :rows="10" type="textarea"/>
      </el-form-item>
      <!-- 教师头像：TODO --><!-- 教师头像 -->
      <el-form-item label="教师头像">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="String(teacher.avatar)"/>
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow=true">更换头像
        </el-button>
        <!--
            @close：关闭上传组件
            @crop-upload-success：上传成功后的回调 -->
        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/eduoss/fileoss'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacher from '@/api/cms/teacher'
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

export default {
  components: {
    ImageCropper, PanThumb
  },
  data() {
    return {
      // 存放教师信息
      teacher: {
        name: '',
        jobTitle: '',
        gender: undefined,
        career: '',
        intro: '',
        avatar: 'https://zfy-lnlg.oss-cn-beijing.aliyuncs.com/v2-6afa72220d29f045c15217aa6b275808_hd.png'
      },
      imagecropperShow: false, // 上传弹框是否显示
      saveBtnDisabled: false, // 保存按钮是否禁用,
      imagecropperKey: 0, // 上传组件唯一标识
      BASE_API: process.env.BASE_API, // 获取端口号 dev.env.js
      ucenterMemberVos: [],
      // 表单校验
      formRules: {
            name: [
                { required: true, message: '请输入姓名', trigger: 'blur' }
            ],
            intro: [
                { required: true, message: '请输入讲师介绍', trigger: 'blur' }
            ]
        }
    }
  },
  watch: {
    $route(to, from) { // 监听路由变化方式
      this.init()
    }
  },
  created() {
    this.init()
  },
  methods: {
    close() { // 关闭上传方法
      this.imagecropperShow = false
      // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    },
    cropSuccess(data) {
      // console.log(data)
      this.imagecropperShow = false
      this.teacher.avatar = data.url
      // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    },
    init() {
      // 判断是否有id
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.getInfo(id)
      } else {
        // 做添加 清空表单
        this.teacher = {
          name: '',
          jobTitle: '',
          gender: undefined,
          career: '',
          intro: '',
          avatar: 'https://zfy-lnlg.oss-cn-beijing.aliyuncs.com/v2-6afa72220d29f045c15217aa6b275808_hd.png'
        }
      }
    },
    // 根据教师id查询方法
    getInfo(id) {
      teacher.getTeacherInfo(id)
        .then(response => {
          console.log(response);
          this.teacher = response.data.item
        })
    },
    saveOrUpdate() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.saveBtnDisabled = true
          // 判断teacher中是否有id 修改或添加
          if (!this.teacher.id) {
            // 添加
            this.saveData()
          } else {
            this.updateTeacher()
          }
        }else {
          this.$message.error('请正确输入表单');
        }
      })
    },
    // 修改教师方法
    updateTeacher() {
      teacher.updateTeacher(this.teacher)
        .then(response => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.$router.push({ path: '/teacher/table' })
        }).catch(response => {
          this.$message({
            type: 'error',
            message: '修改失败'
          })
        })
    },
    // 保存
    saveData() {
      teacher.addTeacher(this.teacher)
        .then(response => { // 添加成功
          this.$message({
            type: 'success',
            message: '保存成功!'
          })
          // 路由跳转
          this.$router.push({ path: '/teacher/table' })
        }).catch(response => {
          this.$message({
            type: 'error',
            message: '保存失败'
          })
        })
    }
  }
}
</script>
